package scct.hys.dao;

import scct.hys.model.Order;
import scct.hys.utils.DBUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {

    public List<Order> getAllOrder(){
        List<Order> orderList=new ArrayList<>();
        Connection conn = DBUtils.getConnection();
        String sql = "  select m.id,c.name,m.name,m.director,releasedate,s.starttime,duration,price,type  from cm_cinema c left join cm_movie m on c.id=m.id left join cm_screening s on c.id=s.id ";
        PreparedStatement pstmt = null;
        System.out.println(sql);
        try {
            pstmt=conn.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                int movieid=rs.getInt("m.id");
                String cinemaName=rs.getString("c.name");
                String movieName=rs.getString("m.name");
                String movieDirector=rs.getString("director");
                String movieReleasedate=rs.getString("releasedate");
                String screeningStarttime=rs.getString("s.starttime");
                String movieDuration=rs.getString("duration");
                String priceScreening=rs.getString("price");
                String movieType=rs.getString("type");
                orderList.add(new Order(movieid,cinemaName,movieName,movieDirector,movieReleasedate,screeningStarttime,movieDuration,priceScreening,movieType));
            }
            DBUtils.closeConnection(conn);
        }catch (SQLException e){
            e.printStackTrace();
        }
return orderList;
    }

}
